﻿using Data.Service.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data.Service
{
    public class UsersData
    {
        public static SYTDataBaseEntities db = new SYTDataBaseEntities();

        //根据用户memberId获取用户信息。
        public static Users GetUser(string memberId)
        {
            return db.Users.Where(M => M.memberId == memberId).First();
        }
        //获取当前用户集合
        public static List<Users> GetUsers(string type)
        {       
            if (type == "AutoResend")
            {
                var requst = from u in db.Users
                             join s in db.Intelligence_AutoResend.Where(M => M.IsResend == true)
                             on u.memberId equals s.MemberId
                             select u;
                return requst.ToList();
            }
            else if (type == "AutoAppraise")
            {
                var requst = from u in db.Users
                             join s in db.Intelligence_AutoAppraise.Where(M => M.IsAutoAppraise == true)
                             on u.memberId equals s.MemberId
                             select u;
                return requst.ToList();
            }
            else if (type == "AutoWindow")
            {
                var requst = from u in db.Users
                             join s in db.Intelligence_AutoWindow.Where(M => M.IsAutoWindow == true)
                             on u.memberId equals s.MemberId
                             select u;
                return requst.ToList();
            }
            else if (type == "AutoStock")
            {
                var requst = from u in db.Users
                             join s in db.Intelligence_AutoStock.Where(M => M.IsAutoStock == true)
                             on u.memberId equals s.MemberId
                             select u;
                return requst.ToList();
            }
            return new List<Users>();                      
        }

        //判断是否是新用户，是添加一条数据，否 更新数据。
        public static Users UpdateUserAllbbInfo(string aliId, string memberId, string refresh_token_timeout, string resource_owner, string access_token, string refresh_token, out bool isNOone)
        {          
            Users currtues = db.Users.Where(M => M.aliId == aliId && M.memberId == memberId).First();
            if (currtues == null)
            {
                Users newuser = new Users();
                newuser.aliId = aliId;
                newuser.memberId = memberId;
                newuser.refresh_token_timeout = refresh_token_timeout;
                newuser.resource_owner = resource_owner;
                newuser.access_token = access_token;
                newuser.refresh_token = refresh_token;
                newuser.aliId = aliId;
                db.Users.Add(newuser);
                isNOone = BaseServer<Users>.AddEntity(newuser);
                return newuser;
            }
            else
            {
                currtues.aliId = aliId;
                currtues.memberId = memberId;
                currtues.refresh_token_timeout = refresh_token_timeout;
                currtues.resource_owner = resource_owner;
                currtues.access_token = access_token;
                currtues.refresh_token = refresh_token;
                currtues.CreateTime = DateTime.Now;
                isNOone = BaseServer<Users>.UpdateEntity(currtues);

                return currtues;
            }          
        }
    }
}
